package com.zy.garbage.controller;

import com.google.code.kaptcha.Producer;
import com.zy.garbage.enums.PermsEnum;
import com.zy.garbage.enums.ResultEnum;
import com.zy.garbage.enums.SessionEnum;
import com.zy.garbage.exception.CommonException;
import com.zy.garbage.form.AccountCheckForm;
import com.zy.garbage.form.AccountForm;
import com.zy.garbage.form.UpdateAccountForm;
import com.zy.garbage.service.AccountService;
import com.zy.garbage.util.ResultUtil;
import com.zy.garbage.vo.AccountVO;
import com.zy.garbage.form.CreateAccountForm;
import com.zy.garbage.vo.ResultVO;
import com.zy.garbage.vo.UserInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user")
public class AccountController extends BaseController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private Producer kaptchaProducer;


    @GetMapping("/verify")
    public ResultVO verify() {
        return ResultUtil.success(null);
    }

    @PostMapping("/login")
    public ResultVO login(@Validated @RequestBody AccountForm accountForm,  HttpServletResponse response) {
        SessionEnum session = SessionEnum.getInstance();
        AccountVO accountVO;
        try {
            accountVO = accountService.login(accountForm, session);
            return ResultUtil.success(accountVO);
        } catch (CommonException e) {
            throw new CommonException(e.getCode(), e.getMessage());
        }
    }

    @PostMapping("/create")
    public ResultVO create(@Validated @RequestBody CreateAccountForm createAccountForm, HttpServletResponse response) {
        try {
            accountService.save(createAccountForm);
            return ResultUtil.success(null);
        } catch (CommonException e) {
            throw new CommonException(e.getCode(), e.getMessage());
        }
    }

    @PutMapping("/updatePwd")
    public ResultVO updatePwd(@Validated @RequestBody AccountForm accountForm) {
        try {
            accountService.updatePwd(accountForm);
            return ResultUtil.success(null);
        } catch (CommonException e) {
            throw new CommonException(e.getCode(), e.getMessage());
        }
    }

    @PutMapping("/update")
    public ResultVO update(@Validated @RequestBody UpdateAccountForm updateAccountForm) {
        try {
            accountService.update(updateAccountForm);
            return ResultUtil.success(null);
        } catch (CommonException e) {
            throw new CommonException(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("/showInfo/{userPhone}")
    public ResultVO showInfo(
            @PathVariable("userPhone") String userPhone) {
        try {
            UserInfoVO userInfoVO = accountService.findByUserPhone(userPhone);
            return ResultUtil.success(userInfoVO);
        } catch (CommonException e) {
            throw new CommonException(e.getCode(), e.getMessage());
        }
    }


    @GetMapping("/allUser")
    public ResultVO allUser() {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                List<UserInfoVO> userInfoVOList = accountService.findAll();
                return ResultUtil.success(userInfoVOList);
            } catch (Exception e) {
                System.out.println(e.toString());
                return ResultUtil.error("查询失败");
            }

        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @PutMapping("/setPwd")
    public ResultVO setPwd(@Validated @RequestBody AccountForm accountForm) {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                accountService.setPwd(accountForm);
                return ResultUtil.success(null);
            } catch (Exception e) {
                return ResultUtil.error("修改失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);

    }

    @GetMapping("/kaptcha")
    public void getKaptcha(HttpServletResponse response) {
        SessionEnum session = SessionEnum.getInstance();
        // 生成验证码
        String text = kaptchaProducer.createText();
        BufferedImage image = kaptchaProducer.createImage(text);
        // 将验证码存入session
        session.setCode(text);
        // 将图片输出给浏览器
        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            ImageIO.write(image, "png", os);
        } catch (IOException e) {

        }
    }

    @PutMapping("/check")
    public ResultVO check(@Validated @RequestBody AccountCheckForm accountCheckForm) {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                accountService.check(accountCheckForm);
                return ResultUtil.success(null);
            } catch (CommonException e) {
                throw new CommonException(e.getCode(), e.getMessage());
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);

    }


}
